package cn.tedu.boot23.controller;

import cn.tedu.boot23.entity.Hero;
import cn.tedu.boot23.mapper.HeroMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.ResultSet;
import java.util.List;

@Controller
public class HeroController {
    @Autowired
    HeroMapper mapper;
    //增
    @RequestMapping("/add")
    @ResponseBody
    public String add(Hero hero){
        System.out.println("hero = " + hero);
        mapper.insert(hero);
        return "添加成功！<a href='/index.html'>首页</a>";
    }

    //删除
    @RequestMapping("/delete")
    @ResponseBody
    public String deleteByName(String name){
        mapper.deleteByName(name);
        return "删除成功！<a href='/index.html'>首页</a>";
    }

    //修改
    @RequestMapping("/update")
    @ResponseBody
    public String update(Hero hero){
        mapper.update(hero);
        return "修改成功！<a href='/index.html'>首页</a>";
    }

    @RequestMapping("/select")
    @ResponseBody
    public String select(){
        List<Hero> list = mapper.select();
        String html="<table border=1>";
        html+="<caption>英雄列表</caption>";
        html+="<tr><th>编号</th><th>英雄名/th><th>价格</th></tr>";
        for(Hero h : list){
            html+="<tr>";
            html+="<td>"+h.getId()+"</td>";
            html+="<td>"+h.getName()+"</td>";
            html+="<td>"+h.getMoney()+"</td>";
            html+="</tr>";
        }
        html+="</table>";
        return html;
    }
}
